<template>
    <div>
        <el-row :gutter="20">
          <el-col :span="12">
              <el-card>
                   <div slot="header" class="clearfix">
                        <span>字典列表</span>
                    </div>
                    <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="100px">
                        <el-form-item label="字典编号：" prop="code">
                            <el-input v-model="queryParams.code" placeholder="字典编号" size="small"></el-input>
                        </el-form-item>
                        <el-form-item label="字典名称：" prop="dictValue">
                            <el-input v-model="queryParams.dictValue" placeholder="字典名称" size="small"></el-input>
                        </el-form-item>
                        <el-form-item >
                            <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                            <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
                        </el-form-item>
                    </el-form>
                    <el-row :gutter="10">
                        <el-col :span="24">
                            <div style="margin-bottom: 10px">
                                <el-button type="primary" icon="el-icon-plus" size="small" @click="handleLeftAdd">新增</el-button>
                                <el-button type="danger" plain icon="el-icon-delete" size="small" @click="handleDelete($refs.leftTableRef.selection)">删除</el-button>
                            </div>
                        </el-col>
                    </el-row>
                    <el-table :data="tableDataLeft" border style="width: 100%" size="small" ref="leftTableRef" @row-click="handleRowClick">
                        <el-table-column type="selection" width="50"></el-table-column>
                        <el-table-column type="index" label="#" width="50"></el-table-column>
                        <el-table-column label="字典编号" prop="code"></el-table-column>
                        <el-table-column label="字典名称" prop="dictValue"></el-table-column>
                        <el-table-column label="封存" prop="isSealed">
                            <template slot-scope="{row}">
                                <el-tag type="primary" v-if="row.isSealed===1">是</el-tag>
                                <el-tag type="primary" v-else>否</el-tag>
                            </template>
                        </el-table-column>
                         <el-table-column label="操作" width="150" align="center">
                            <template slot-scope="scope">
                                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleLeftEdit(scope.row)">编辑</el-button>
                                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDel(scope.row.id)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                     <el-pagination
                        v-show="leftTotal>0"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        background
                        :current-page="1"
                        :page-sizes="[10, 20, 30, 40,50,100]"
                        :page-size="10"
                        layout="total, sizes, prev, pager, next, jumper"
                        class="pagination-container"
                        :total="leftTotal">
                    </el-pagination>
              </el-card>    
          </el-col>
           <el-col :span="12">
              <el-card>
                   <div slot="header" class="clearfix">
                        <span>[{{rightTitle}}] 字典详情</span>
                    </div>
                    <el-form :model="rightQueryParams" ref="rightQueryParams" :inline="true" label-width="100px">
                        <el-form-item label="字典编号：" prop="code">
                            <el-input v-model="rightQueryParams.code" placeholder="字典编号" size="small"></el-input>
                        </el-form-item>
                        <el-form-item label="字典名称：" prop="dictValue">
                            <el-input v-model="rightQueryParams.dictValue" placeholder="字典名称" size="small"></el-input>
                        </el-form-item>
                        <el-form-item >
                            <el-button type="primary" icon="el-icon-search" size="small" @click="handleRightQuery">搜索</el-button>
                            <el-button icon="el-icon-delete" size="small" @click="resetRightQuery('rightQueryParams')">清空</el-button>
                        </el-form-item>
                    </el-form>
                    <el-row :gutter="10">
                        <el-col :span="24">
                            <div style="margin-bottom: 10px">
                                <el-button type="primary" icon="el-icon-plus" size="small" @click="handleRightAdd">新增</el-button>
                                <el-button type="danger" plain icon="el-icon-delete" size="small" @click="handleDelete($refs.rightTableRef.selection)">删除</el-button>
                            </div>
                        </el-col>
                    </el-row>
                        <el-table :data="tableDataRight" border style="width: 100%" size="small" ref="rightTableRef">
                        <el-table-column type="selection" width="50"></el-table-column>
                        <el-table-column type="index" label="#" width="50"></el-table-column>
                        <el-table-column label="字典编号" prop="code"></el-table-column>
                        <el-table-column label="字典名称" prop="dictValue"></el-table-column>
                        <el-table-column label="字典键值" prop="dictKey"></el-table-column>
                        <el-table-column label="字典排序" prop="sort"></el-table-column>
                        <el-table-column label="封存" prop="isSealed">
                            <template slot-scope="{row}">
                                <el-tag type="primary" v-if="row.isSealed===1">是</el-tag>
                                <el-tag type="primary" v-else>否</el-tag>
                            </template>
                        </el-table-column>
                         <el-table-column label="操作" width="150" align="center">
                            <template slot-scope="scope">
                                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleRightEdit(scope.row)">编辑</el-button>
                                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                     <el-pagination
                        v-show="rightTotal>0"
                        @size-change="handleRightSizeChange"
                        @current-change="handleRightCurrentChange"
                        background
                        :current-page="1"
                        :page-sizes="[10, 20, 30, 40,50,100]"
                        :page-size="10"
                        layout="total, sizes, prev, pager, next, jumper"
                        class="pagination-container"
                        :total="rightTotal">
                    </el-pagination>
              </el-card>    
          </el-col>
        </el-row>
       <el-dialog :title="formTitle" :visible.sync="formVisible" width="50%">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item  prop="code" label="字典编号：" required>
                            <el-input v-model="addForm.code" autocomplete="off" size="mini" placeholder="请输入字典编号" :disabled="mode==='subAdd' || mode==='subEdit'"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item  prop="dictValue" label="字典名称：" required>
                            <el-input v-model="addForm.dictValue" autocomplete="off" size="mini" placeholder="请输入字典名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item  prop="parentId" label="上级字典：" v-if="mode==='subAdd' || mode==='subEdit'">
                            <el-select v-model="addForm.parentId" placeholder="请选择" size="small" disabled>
                                <el-option :label="addForm.$parentId" :value="addForm.parentId"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item  prop="dictKey" label="字典键值：" required v-if="mode==='subAdd' || mode==='subEdit'">
                            <el-input v-model="addForm.dictKey" size="mini" style="width: 100%"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item  prop="sort" label="字典排序：" required>
                            <el-input-number v-model="addForm.sort" size="mini" style="width: 100%" controls-position="right"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item  prop="isSealed" label="封存：" required>
                            <el-switch
                                v-model="addForm.isSealed"
                                active-text="是"
                                inactive-text="否">
                            </el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item  prop="remark" label="字典备注：" >
                            <el-input v-model="addForm.remark" autocomplete="off" size="mini" placeholder="请输入字典备注"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSubmit('addForm')" size="small">保存</el-button>
                <el-button @click="formVisible = false" size="small">取消</el-button>
            </div>
         </el-dialog>
    </div>
</template>

<script>
import {getParentList,getChildList,save,remove} from '@/api/system/dict.api.js'
    export default {
        data() {
            return {
                queryParams: {},
                total: 0,
                tableDataLeft: [],
                tableDataRight: [],
                formTitle: '新增',
                addForm: {},
                formVisible: false,
                rules: {
                    code: [{ required: true, message: '请输入字典编号', trigger: 'blur' }],
                    dictValue: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
                    isSealed: [{ required: true, message: '请选择封存', trigger: 'blur' }],
                    sort: [{ required: true, message: '请输入字典排序', trigger: 'blur' }],
                },
                dictArr: [],
                rightTitle: '暂无',
                rightQueryParams: {
                    current: 1,
                    size: 10,
                    parentId: -1
                },
                leftTotal: 0,
                rightTotal: 0,
                mode: 'add'
            }
        },
        mounted() {
            this.rightQueryParams.parentId = -1;
            this.getList();
            this.getSubList();
        },
        methods: {
            // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
            // 搜索
            handleRightQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetRightQuery(formName) {
                this.$refs[formName].resetFields();
                this.getSubList();
            },
            handleRightSizeChange(val) {
                this.rightQueryParams.size = val;
                this.getSubList();
            },
            handleRightCurrentChange(val) {
                this.rightQueryParams.current = val;
                this.getSubList();
            },
            handleLeftAdd() {
                this.formVisible = true;
            },
            handleLeftEdit(row) {
                this.formTitle = '编辑';
                this.mode = 'edit';
                this.addForm = JSON.parse(JSON.stringify(row))
                this.formVisible = true;
            },
            handleRightEdit(row) {
                this.formTitle = '编辑';
                this.mode = 'subEdit';
                this.addForm = JSON.parse(JSON.stringify(row));
                this.formVisible = true;
            },
            handleSubmit(formName) {
                 this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        this.addForm.isSealed?this.addForm.isSealed=1:this.addForm.isSealed=0
                        this.addForm.isSealed === 0?this.addForm.$isSealed = '否':'是'
                           const result =  await save(this.addForm)
                            if(result.code === 200) {
                                this.$message({
                                    message: '保存成功',
                                    type: 'success'
                                });
                               this.formVisible = false;
                            }
                    } else {
                        return false;
                    }
                });
            },
            handleRightAdd() {
                this.formTitle = '新增';
                this.formVisible = true;
                this.mode = 'subAdd';
            },
            async getSubList() {
                const result = await getChildList(this.rightQueryParams);
                if(result.code === 200) {
                    this.tableDataRight = result.data.records;
                    this.rightTotal = result.data.total;
                }
            },
            async getList() {
                const result = await getParentList(this.queryParams);
                if(result.code === 200) {
                    this.tableDataLeft = result.data.records;
                    this.leftTotal = result.data.total;
                }
            },
            async handleRowClick(row) {
                this.rightTitle = row.dictValue;
                this.addForm.code = row.code;
                this.addForm.$parentId = row.dictValue;
                this.addForm.parentId = row.parentId;
                this.rightQueryParams.parentId = row.id;
                const result = await getChildList(this.rightQueryParams);
                if(result.code === 200) {
                    this.tableDataRight = result.data.records;
                    this.rightTotal = result.data.total;
                }
            },
            handleDelete(ids) {
                let deptIds;
                typeof ids === 'string'?deptIds=ids:deptIds=ids.map(item=>item.id).toString();
                if(deptIds.length===0) {
                    this.$message({
                        type: 'error',
                        message: '请至少选择一条数据'
                    })
                    return;
                }
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await remove(deptIds);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                          this.getList();
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
        }
    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>